package com.slmxz.tms.document.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.slmxz.tms.common.base.IBaseEnum;
import com.slmxz.tms.common.component.RedisUserService;
import com.slmxz.tms.common.constant.SystemConstants;
import com.slmxz.tms.common.enums.BizMessageEnum;
import com.slmxz.tms.common.model.Option;
import com.slmxz.tms.core.exception.Asserts;
import com.slmxz.tms.core.exception.BizException;
import com.slmxz.tms.common.result.ResultCode;
import com.slmxz.tms.core.service.DataScopeService;
import com.slmxz.tms.document.enums.ContractorTypeEnum;
import com.slmxz.tms.document.mapper.ContractorMapper;
import com.slmxz.tms.document.model.entity.Contractor;
import com.slmxz.tms.document.model.vo.ContractorVO;
import com.slmxz.tms.document.service.ContractorService;
import com.slmxz.tms.system.model.vo.UserInfoVO;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * 承运商信息服务实现类
 * @author sunyao
 * @Date 2024-11-08
 */
@Service
@RequiredArgsConstructor
public class ContractorServiceImpl extends ServiceImpl<ContractorMapper, Contractor> implements ContractorService, DataScopeService<Contractor> {

    /**
    * 获取承运商信息分页列表
    * @return {@link IPage< ContractorVO >} 承运商信息分页列表
    */
    @Override
    public IPage<ContractorVO> selectContractorPage(IPage<ContractorVO> page,ContractorVO contractor) {
        UserInfoVO user = RedisUserService.currentUser();
        try{
            this.dataScopeHandler(contractor);
        }catch (Exception e){
            contractor.setScopeNo(user.getScopeNo());
            contractor.setDataOperate(SystemConstants.OPERATE_EQUALS);
        }
        contractor.setCompanyId(user.getCompanyId());
        List<ContractorVO> contractors = this.baseMapper.selectContractorPage(page,contractor);
        if(CollectionUtil.isNotEmpty(contractors)){
            contractors.forEach(item -> {
                item.setContractorTypeLabel(IBaseEnum.getLabelByValue(item.getContractorType(), ContractorTypeEnum.class));
            });
        }
        return page.setRecords(contractors);
    }
    
    /**
     * @param id 承运商信息ID
     * @return
     */
    @Override
    public ContractorVO selectContractorDetail(Long id) {
        Contractor entity = this.getById(id);
        ContractorVO contractor = BeanUtil.copyProperties(entity,ContractorVO.class);
        return contractor;
    }
    
    /**
     * 新增承运商信息
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean saveContractor(Contractor contractor) {
        return this.save(contractor);
    }
    
    /**
     * 更新承运商信息
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean updateContractor(Contractor contractor) {
        return this.updateById(contractor);
    }
    
    /**
     * 删除承运商信息
     *
     * @param ids 承运商信息ID，多个以英文逗号(,)分割
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean deleteContractors(String ids) {
        Asserts.fail(StrUtil.isBlank(ids), BizMessageEnum.PARAM_IS_NULL);

        // 逻辑删除
        List<Long> idList = Arrays.stream(ids.split(","))
                .map(Long::parseLong)
                .toList();
        return this.removeByIds(idList);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean activeOrLogout(ContractorVO contractor) {
        //激活或者注销业务数据
        Asserts.fail(CollectionUtil.isEmpty(contractor.getIds()) || contractor.getIsDeleted() == null, BizMessageEnum.PARAM_IS_NULL);

        List<Contractor>  contractorList = new ArrayList<>(contractor.getIds().size());
        contractor.getIds().forEach(id -> {
            Contractor contractorParam = new Contractor();
            contractorParam.setId(id);
            contractorParam.setIsDeleted(contractor.getIsDeleted());
            contractorList.add(contractorParam);
        });
        this.updateBatchById(contractorList,10);
        return true;
    }

    @Override
    public List<Option<String>> selectOption(ContractorVO contractor) {
        contractor.setMaxSize(1000);
        return this.baseMapper.selectOption(contractor);
    }
}
